<?php
/**
 * 排序通用验证
 *
 * @author Zeon <scplay@gmail.com>
 * @date 2018/9/7 10:47
 * @copyright PanxSoft Inc.
 */

namespace App\Http\Requests\Concerns;

use Illuminate\Validation\Rule;

/**
 * Trait OrderBysRule
 *
 * @property array|null order_bys - 多栏排序数组
 *
 * @property string|null order_by - 排序字段
 * @property string|null sort - 排序
 *
 * @package App\Http\Requests\Concerns
 */
trait OrderBysRule
{
    /**
     * @param array|null $valid_columns
     *
     * @return array
     */
    protected function orderBysRule(array $valid_columns = null)
    {
        $rule_in = $valid_columns ? [Rule::in($valid_columns)] : [];

        return [
            'order_bys'            => 'nullable|array',
            'order_bys.*.order_by' => array_merge(['required', 'string'], $rule_in),
            'order_bys.*.sort'     => ['required', 'string', 'regex:/^(DESC|ASC)$/i'],
        ];
    }
    /**
     * @param array|null $valid_columns
     *
     * @return array
     */
    protected function orderByRule(array $valid_columns = null)
    {
        $rule_in = $valid_columns ? [Rule::in($valid_columns)] : [];

        return [
            'order_by' => array_merge(['nullable', 'string'], $rule_in),
            'sort'     => ['nullable', 'string', 'regex:/^(DESC|ASC)$/i'],
        ];
    }
}